package com.cnhis.cloudhealth.clinical.osworks.commonsql.bo;


import com.cnhis.cloudhealth.clinical.osworks.commonsql.dao.OsCommonSqlDao;
import com.cnhis.cloudhealth.commons.Mappers.ModelVo;
import com.cnhis.cloudhealth.commons.newid.GetNewId;
import com.cnhis.cloudhealth.commons.util.DateUtil;
import com.cnhis.cloudhealth.commons.utils.GlobalVariable;
import com.cnhis.cloudhealth.commons.utils.Page;
import com.cnhis.cloudhealth.commons.utils.StaticKeys;
import com.cnhis.cloudhealth.commons.vo.exception.BaseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class OsCommonSqlBo {
	
	@Autowired
	private OsCommonSqlDao osCommonSqlDao;
	
	@Resource
	private GetNewId getNewId;

	@SuppressWarnings("rawtypes")
	public List   operatingRoom(Map<String, Object> map) {
		List list = null;
		try{
			 list = osCommonSqlDao.operatingRoom(map);
		}catch(Exception e){
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		return list;
	}

	public ModelVo get1669(Map<String, Object> map) {
		ModelVo mv = new ModelVo();
		try{
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String enddate = sdf.format(new Date());
			Integer syspar = DateUtil.timeRange(GlobalVariable.StringIsNull(map.get("startDate")),GlobalVariable.StringIsNull(enddate));
			Page page = null;
			if(syspar.equals(0)) {
				page = osCommonSqlDao.get1669(map, GlobalVariable.IntegerIsNull(map.get("page")), GlobalVariable.IntegerIsNull(map.get("rows")));
			}else{
				page = osCommonSqlDao.get1669_view(map, GlobalVariable.IntegerIsNull(map.get("page")), GlobalVariable.IntegerIsNull(map.get("rows")));
			}
			mv.setRows(page.getList());
			mv.setTotal(page.getTotalNumber());
			//recordsFiltered
			mv.setRecordsFiltered(page.getTotalNumber());
			mv.setRecordsTotal(page.getTotalNumber());
		}catch(Exception e){
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		mv.setResult(StaticKeys.OH_SUCCESS);
		return mv;
	}

	public String loaddate() {
		// TODO Auto-generated method stub
		String adate= osCommonSqlDao.getSysParameters(100,106004,1);
		Calendar calendar = Calendar.getInstance(); 
		if(!adate.equals(0)){
			calendar.add(Calendar.DAY_OF_WEEK, - Integer.parseInt(adate)); 
		}else{
			calendar.add(Calendar.DAY_OF_WEEK, - 60);    
		}
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String dateStr = sdf.format(calendar.getTime());
		return dateStr;
	}

	@SuppressWarnings("rawtypes")
	public List get834(Map<String, Object> map) {
		List list=null;
		try {
			list = osCommonSqlDao.get834(map);
		} catch (Exception e) {
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		return list;
	}

	@SuppressWarnings("rawtypes")
	public List get16618(Map<String, Object> map) {
		List list=null;
		try {
			list = osCommonSqlDao.get16618(map);
		} catch (Exception e) {
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		return list;
	}

	@SuppressWarnings("rawtypes")
	public List get1176(Map<String, Object> map) {
		List list=null;
		try {
			Integer acf01 = GlobalVariable.IntegerIsNull(map.get("ACF01"));
			if(acf01 == 1){
				list = osCommonSqlDao.get1176Acf01(map);
			}else{
				list = osCommonSqlDao.get1176Acf02(map);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		return list;
	}

	@SuppressWarnings("rawtypes")
	public List get1670(Map<String, Object> map) {
		List list=null;
		try {
			Integer syspar = DateUtil.timeRange(GlobalVariable.StringIsNull(map.get("startdate")),GlobalVariable.StringIsNull(map.get("enddate")));
			if(syspar.equals(0)){
				list = osCommonSqlDao.get1670(map);
			}else{
				list = osCommonSqlDao.get1670_view(map);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		return list;
	}

	@SuppressWarnings("rawtypes")
	public List get1662(Map<String, Object> map) {
		// TODO Auto-generated method stub
		List list=null;
		try {
			List list1 = osCommonSqlDao.getVAT1(map);
			String cbm01="";
			if(list1.size() > 0){
				cbm01 = osCommonSqlDao.getCBM01VAF1(map);
				map.put("CBM01", cbm01);
				list = osCommonSqlDao.get1662VAF1(map);
			}else{
				cbm01 = osCommonSqlDao.getCBM01VAF2(map);
				map.put("CBM01", cbm01);
				list = osCommonSqlDao.get1662VAF2(map);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		return list;
	}

	public ModelVo get814(Map<String, Object> map) {
		ModelVo mv = new ModelVo();
		try {
			//list = osCommonSqlDao.get814(map);
			Page page = osCommonSqlDao.get814(map,GlobalVariable.IntegerIsNull(map.get("page")),GlobalVariable.IntegerIsNull(map.get("rows")));
			mv.setRows(page.getList());
			mv.setTotal(page.getTotalNumber());
			//recordsFiltered
			mv.setRecordsFiltered(page.getTotalNumber());
			mv.setRecordsTotal(page.getTotalNumber());
		}catch(Exception e){
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		mv.setResult(StaticKeys.OH_SUCCESS);
		return mv;
	}

	@SuppressWarnings("rawtypes")
	public ModelVo get1663(Map<String, Object> map) {
		// TODO Auto-generated method stub
		ModelVo mv = new ModelVo();
		try {
			//list = osCommonSqlDao.get1663(map);
			Page page = osCommonSqlDao.get1663(map,GlobalVariable.IntegerIsNull(map.get("page")),GlobalVariable.IntegerIsNull(map.get("rows")));
			mv.setRows(page.getList());
			mv.setTotal(page.getTotalNumber());
			//recordsFiltered
			mv.setRecordsFiltered(page.getTotalNumber());
			mv.setRecordsTotal(page.getTotalNumber());
		} catch (Exception e) {
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		mv.setResult(StaticKeys.OH_SUCCESS);
		return mv;
	}

	@SuppressWarnings("rawtypes")
	public ModelVo get1684(Map<String, Object> map) {
		// TODO Auto-generated method stub
		ModelVo mv = new ModelVo();
		try {
			//list = osCommonSqlDao.get1684(map);
			Page page = osCommonSqlDao.get1684(map,GlobalVariable.IntegerIsNull(map.get("page")),GlobalVariable.IntegerIsNull(map.get("rows")));
			mv.setRows(page.getList());
			mv.setTotal(page.getTotalNumber());
			//recordsFiltered
			mv.setRecordsFiltered(page.getTotalNumber());
			mv.setRecordsTotal(page.getTotalNumber());
		} catch (Exception e) {
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		mv.setResult(StaticKeys.OH_SUCCESS);
		return mv;
	}

	@SuppressWarnings("rawtypes")
	public List get7257(Map<String, Object> map) {
		// TODO Auto-generated method stub
		List list=null;
		try {
			Integer syspar = DateUtil.timeRange(GlobalVariable.StringIsNull(map.get("startdate")),GlobalVariable.StringIsNull(map.get("enddate")));
			if(syspar.equals(0)) {
				list = osCommonSqlDao.get7257(map);
			}else{
				list = osCommonSqlDao.get7257_view(map);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		return list;
	}

	@SuppressWarnings("rawtypes")
	public List get802(Map<String, Object> map) {
		// TODO Auto-generated method stub
		List list=null;
		try {
			list = osCommonSqlDao.get802(map);
		} catch (Exception e) {
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		return list;
	}

	@SuppressWarnings("rawtypes")
	public ModelVo get8027(Map<String, Object> map) {
		// TODO Auto-generated method stub
		ModelVo mv = new ModelVo();
		try {
			//list = osCommonSqlDao.get8027(map);
			Page page = osCommonSqlDao.get8027(map,GlobalVariable.IntegerIsNull(map.get("page")),GlobalVariable.IntegerIsNull(map.get("rows")));
			mv.setRows(page.getList());
			mv.setTotal(page.getTotalNumber());
			//recordsFiltered
			mv.setRecordsFiltered(page.getTotalNumber());
			mv.setRecordsTotal(page.getTotalNumber());
		} catch (Exception e) {
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		mv.setResult(StaticKeys.OH_SUCCESS);
		return mv;
	}

	@SuppressWarnings("rawtypes")
	public List get806() {
		// TODO Auto-generated method stub
		List list=null;
		try {
			list = osCommonSqlDao.get806();
		} catch (Exception e) {
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		return list;
	}

	@SuppressWarnings("rawtypes")
	public List get805() {
		// TODO Auto-generated method stub
		List list=null;
		try {
			list = osCommonSqlDao.get805();
		} catch (Exception e) {
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		return list;
	}
	
	@SuppressWarnings("rawtypes")
	public List get804() {
		// TODO Auto-generated method stub
		List list=null;
		try {
			list = osCommonSqlDao.get804();
		} catch (Exception e) {
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		return list;
	}
	
	@SuppressWarnings("rawtypes")
	public List get15469() {
		// TODO Auto-generated method stub
		List list=null;
		try {
			list = osCommonSqlDao.get15469();
		} catch (Exception e) {
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		return list;
	}

	public List get15468() {
		List list=null;
		try {
			list=osCommonSqlDao.get15468();
			//list = osCommonSqlDao.get8027(map);
			/*Page page = osCommonSqlDao.get15468(map,GlobalVariable.IntegerIsNull(map.get("page")),GlobalVariable.IntegerIsNull(map.get("rows")));
			mv.setRows(page.getList());
			mv.setTotal(page.getTotalNumber());
			//recordsFiltered
			mv.setRecordsFiltered(page.getTotalNumber());
			mv.setRecordsTotal(page.getTotalNumber());*/
		} catch (Exception e) {
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		//mv.setResult(StaticKeys.OH_SUCCESS);
		return list;
	}

	@SuppressWarnings("rawtypes")
	public ModelVo getSysParamValue(Map<String, Object> paramMap) {
		ModelVo mv = new ModelVo();
		try {
			List list = osCommonSqlDao.getSysParamValue(paramMap);
			mv.setRows(list);
            mv.setList(list);
		} catch (Exception e) {
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		mv.setResult(StaticKeys.OH_SUCCESS);
		return mv;
	}

	@SuppressWarnings("rawtypes")
	public List get871() {
		List list=null;
		try {
			list=osCommonSqlDao.get871();
		} catch (Exception e) {
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		return list;
	}

	public ModelVo SavedOneUserParameters(Map<String, Object> map) throws Exception {
		ModelVo mo = new ModelVo(StaticKeys.OH_SUCCESS);
/*		map.put("Pid", getNewId.nextId());
		map.put("Uid", getNewId.nextId());
		if (systemParams(map)) {
			insertSysParams(map);
		} else {
			// 删除用户参数
			if (sysParamters(map) && "0".equals(map.get("Privilege").toString())) {
				deleteSYSUserParameters(map);
			} else {
				deleteUserParams(map);
			}
			if ("0".equals(map.get("Privilege").toString())) {
				if (Long.parseLong(map.get("UserID").toString()) > 0) {
					// 增加日志 修改参数
					insertLogAndUpdateParams(map);
				}
			}
			if ("1".equals(map.get("Privilege").toString()) || ("3".equals(map.get("Privilege").toString()))
					|| ("2".equals(map.get("Privilege").toString()))) {
				insertSystemUserParamters(map);
			}
			//修改参数
			
		}*/
		osCommonSqlDao.updateSYSUserParameters(map);
		return mo;
	}
	
	private void deleteSYSUserParameters(Map<String, Object> map) throws Exception {
		osCommonSqlDao.deleteSYSUserParameters(map);
	}

	private void deleteUserParams(Map<String, Object> map) throws Exception {
		osCommonSqlDao.deleteUserParams(map);
	}

	private boolean sysParamters(Map<String, Object> map) throws Exception {
		return osCommonSqlDao.sysParamters(map) > 0;
	}

	private void insertSysParams(Map<String, Object> map) throws Exception {
		osCommonSqlDao.insertSysParams(map);
	}

	private boolean systemParams(Map<String, Object> map) throws Exception {
		return osCommonSqlDao.systemtParams(map) == 0;
	}

	@SuppressWarnings("unchecked")
	private void insertLogAndUpdateParams(Map<String, Object> map) throws Exception {
		map = getParamters(map);
		map.put("IncCount", 1);
		Long EventLogID = execCoreNewIdSYSEventLogs(map);
		map.put("EventLogID", EventLogID);
		map.put("MessageID", 0);
		// 新增系统时间日志
		execSYSEventLogsInsert(map);
		// 修改参数
		updateSysParams(map);
	}

	private void insertSystemUserParamters(Map<String, Object> map) throws Exception {
		//if (queryParams(map)) {
			updateSysParams(map);
		//}
		osCommonSqlDao.insertSystemUserParamters(map);
	}
	
	@SuppressWarnings("unchecked")
	private Map<String, Object> getParamters(Map<String, Object> map) throws Exception {
		Map<String, Object> m = osCommonSqlDao.getParamters(map);
		map.put("EmpName", m.get("empname").toString());
		map.put("Host", m.get("host").toString());
		map.put("IP", m.get("ip").toString());
		m = osCommonSqlDao.getParamNameAndEvent(map);
		map.put("ProgramName", m.get("programname").toString());
		map.put("Event", m.get("event").toString());
		return map;
	}

	private void execSYSEventLogsInsert(Map<String, Object> map) throws Exception {
		osCommonSqlDao.execSYSEventLogsInsert(map);
	}

	private Long execCoreNewIdSYSEventLogs(Map<String, Object> map) throws Exception {
		return osCommonSqlDao.execCoreNewIdSYSEventLogs(map);
	}

	private void updateSysParams(Map<String, Object> map) throws Exception {
		osCommonSqlDao.updateSysParams(map);
	}

	private boolean queryParams(Map<String, Object> map) throws Exception {
		return osCommonSqlDao.queryParams(map) == 0;
	}

	@SuppressWarnings("rawtypes")
	public List get802ED(Map<String, Object> map) {
		List list=null;
		try {
			list = osCommonSqlDao.get802ED(map);
		} catch (Exception e) {
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		return list;
	}

	/**
	 * 获取系统参数值 getSysParamValue
	 * @param ProductId
	 * @param ProgramId
	 * @param ParamNo
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public String getSysParamValue(Integer ProductId,Integer ProgramId,Integer ParamNo) throws Exception{
		Map param = new HashMap();
		param.put("ProductId", ProductId);
		param.put("ProgramId", ProgramId);
		param.put("ParamNo", ParamNo);
		String ss = osCommonSqlDao.getSysParamValueS(param);
		return ss;
	}

	public ModelVo yuanNeiNotice(Map<String, Object> paramMap) {
		ModelVo mv = new ModelVo();
		try{
			Page page=osCommonSqlDao.yuanNeiNotice(paramMap, GlobalVariable.IntegerIsNull(paramMap.get("page")), GlobalVariable.IntegerIsNull(paramMap.get("rows")));
			mv.setRows(page.getList());
			mv.setTotal(page.getTotalNumber());
			mv.setRecordsFiltered(page.getTotalNumber());
			mv.setRecordsTotal(page.getTotalNumber());
			mv.setRecords(page.getTotalNumber());
		}catch(Exception e){
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		mv.setResult(StaticKeys.OH_SUCCESS);
		return mv;
	}

	public String getPValueBck03e(String bck01e) {
		// TODO Auto-generated method stub
		return osCommonSqlDao.getPValueBck03e(bck01e);
	}

	@SuppressWarnings("rawtypes")
	public List get15470() {
		List list=null;
		try {
			list = osCommonSqlDao.get15470();
		} catch (Exception e) {
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		return list;
	}
	
	@SuppressWarnings("rawtypes")
	public List get15471() {
		List list=null;
		try {
			list = osCommonSqlDao.get15471();
		} catch (Exception e) {
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		return list;
	}

	@SuppressWarnings("rawtypes")
	public List get836() {
		List list=null;
		try {
			list = osCommonSqlDao.get836();
		} catch (Exception e) {
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		return list;
	}

	@SuppressWarnings("rawtypes")
	public List get16805() {
		List list=null;
		try {
			list = osCommonSqlDao.get16805();
		} catch (Exception e) {
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		return list;
	}

	@SuppressWarnings("rawtypes")
	public List get16804() {
		List list=null;
		try {
			list = osCommonSqlDao.get16804();
		} catch (Exception e) {
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		return list;
	}

	public List get16618A(Map<String, Object> map) {
		List list=null;
		try {
			list = osCommonSqlDao.get16618A(map);
		} catch (Exception e) {
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		return list;
	}
	
	public List get16618B(Map<String, Object> map) {
		List list=null;
		try {
			list = osCommonSqlDao.get16618B(map);
		} catch (Exception e) {
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		return list;
	}

	@SuppressWarnings("rawtypes")
	public List getVao12(String vAT01, Integer aCF01) {
		List list=null;
		try {
			if(aCF01==1){
				list = osCommonSqlDao.getVao1(vAT01);
			}else{
				list = osCommonSqlDao.getVao2(vAT01);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		return list;
	}
	
	@SuppressWarnings("rawtypes")
	public List getVAE1VAA1(String VAE01) {
		List list=null;
		try {
			list = osCommonSqlDao.getVAE1VAA1(VAE01);
		} catch (Exception e) {
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		return list;
	}

	@SuppressWarnings("rawtypes")
	public List getVAC1VAA1(String VAA07) {
		List list=null;
		try {
			list = osCommonSqlDao.getVAC1VAA1(VAA07);
		} catch (Exception e) {
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		return list;
	}
}
